﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dooshion.Utility
{
    /// <summary>
    /// 针对Http请求进行操作的帮助类
    /// </summary>
    public class HttpHelper
    {
        //Http参数传递类型
        public enum HttpType
        {
            GET,
            POST,
        }
        /// <summary>
        /// 获取页面传递的参数信息
        /// </summary>
        /// <param name="parameter">参数名</param>
        /// <param name="type">传递类型</param>
        /// <returns>返回值</returns>
        public static string GetParameter(string parameter, HttpHelper.HttpType type)
        {
            switch (type)
            {
                case HttpType.POST:
                    if (System.Web.HttpContext.Current.Request.Form[parameter] != null)
                        return System.Web.HttpContext.Current.Request.Form[parameter].ToString();
                    break;
                case HttpType.GET:
                    if (System.Web.HttpContext.Current.Request.QueryString[parameter] != null)
                        return System.Web.HttpContext.Current.Request.QueryString[parameter].ToString();
                    break;
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取当前页绝对路径。
        /// </summary>
        /// <remarks>http://localhost:1897/News/Press/Content.aspx/123?id=1</remarks>
        /// <returns>绝对路径Url</returns>
        public static string GetAbsoluteUrl()
        {
            return System.Web.HttpContext.Current.Request.Url.AbsoluteUri.ToString();
        }

        /// <summary>
        /// 获取当前访问用户Ip
        /// </summary>
        /// <returns>Ip地址</returns>
        public static string GetUserIp()
        {
            return System.Web.HttpContext.Current.Request.UserHostAddress;
        }
    }
}
